<?php

namespace App\Listener;

use App\Event\UserOnlineEvent;
use App\Helpers\Helper;
use App\Helpers\RedisUtil;
use App\Job\HostGreetJob;
use App\Job\PushOnlineTipJob;
use App\Job\SendSysgiftJob;
use App\Job\SysCallPopJob;
use App\Model\GiftBatchLog;
use App\Model\HostPushLog;
use App\Model\User;
use App\Model\UserProfile;
use Hyperf\Event\Annotation\Listener;
use Hyperf\Event\Contract\ListenerInterface;

/**
 * 上线提醒监听
 * @Listener 
 */
class OnlineTipListener implements ListenerInterface
{
    public function listen(): array
    {
        return [
            //上线事件
            UserOnlineEvent::class,
        ];
    }

    /**
     * @param UserRegistered $event
     */
    public function process(object $event)
    {
        //离线暂不处理
        if ($event->onlineStatus == 0 || env('ONLINE_LISTEN_PUSH', 1) == 0) return;

        $userId = $event->userId[0];
        $profile = UserProfile::getProfile($userId);
        //女性上线暂不处理
        if (User::checkOfficial($userId) || !$profile || $profile->gender == 0) return;

        //是否在限制用户中
        if (Helper::checkTempExcluded($userId)) return;

        //男性推送
        //上线提醒（app下滑提醒）
        Helper::pushRedisAsync(new PushOnlineTipJob([
            'user_id' => $userId,
            'profile' => $profile
        ]), 0, 'default');

        //主播系统打招呼
        $checkOffTime = env('HOST_PUSH_OFFTIME', 3600 * 6);
        $checkPushCount = HostPushLog::checkPushedCount($userId, $checkOffTime);
        if (!$checkPushCount) {
            $pushTimes = explode(',', env('SYS_PUSH_HOST_SETTING', '0-1,30-3,120-5,180-5,300-5'));
            foreach ($pushTimes as $batch => $pushSet) {
                $setting = explode('-', $pushSet);
                $delayTime = intval($setting[0]);
                $pushNum = intval($setting[1]);
                log_user_result($userId, "hostGreet==Delay:{$delayTime}==Num:{$pushNum}");
                Helper::pushRedisAsync(new HostGreetJob([
                    'user_id' => $userId,
                    'push_num' => $pushNum,
                    'batch_no' => $batch + 1,
                ]), $delayTime, 'push_msg');
            }
        }

        //用户系统礼物
        $checkOffTime = env('USER_PUSH_OFFTIME', 3600 * 12);
        $checkSendCount = GiftBatchLog::checkSendCount($userId, $checkOffTime);
        if (!$checkSendCount) {
            $pushTimes = explode(',', env('SYS_PUSH_USER_SETTING', '0-1,10-1,20-1'));
            foreach ($pushTimes as $batch => $pushSet) {
                $setting = explode('-', $pushSet);
                $delayTime = intval($setting[0]);
                $pushNum = intval($setting[1]);
                log_user_result($userId, "userPush==Delay:{$delayTime}==Num:{$pushNum}");
                Helper::pushRedisAsync(new SendSysgiftJob([
                    'user_id' => $userId,
                    'push_num' => $pushNum,
                    'batch_no' => $batch + 1
                ]), $delayTime, 'push_msg');
            }
        }

        //系统呼叫弹框
        // $offTime = 120;
        // $cachekey = "sys_call_pop_{$userId}";
        // if (RedisUtil::get($cachekey) != 1) {
        //     User::checkVersion($userId) >= '1.2.7' && Helper::pushRedisAsync(new SysCallPopJob([
        //         'user_id' => $userId,
        //         'profile' => $profile
        //     ]), $offTime, 'push_msg');
        //     RedisUtil::set($cachekey, 1, 24 * 3600);
        // }

        // log_user_result($userId, 'proccess');
    }
}
